package frames;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import banco.Banco;
import objetos.Categoria;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class FrameCategoria extends JFrame {

	private JPanel contentPane;
	private JTextField textCategoria;
	private JButton button;
	
	private Categoria categoria = new Categoria();
	private Banco bd = new Banco();
	private JTextField textNome;

	/**
	 * Launch the application.
	 
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameCategoria frame = new FrameCategoria();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameCategoria(final String acao) {
		setTitle("Categoria");
		getContentPane().setLayout(null);
		
		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(10, 33, 99, 14);
		getContentPane().add(lblNome);
		
		textNome = new JTextField();
		textNome.setColumns(10);
		textNome.setBounds(98, 30, 177, 20);
		getContentPane().add(textNome);
		
		JButton button = new JButton("Salvar");
		button.setBounds(111, 72, 89, 23);
		getContentPane().add(button);
		setTitle("Categoria");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 374, 176);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textCategoria = new JTextField();
		textCategoria.setBounds(75, 37, 267, 20);
		contentPane.add(textCategoria);
		textCategoria.setColumns(10);
		
		button = new JButton("Salvar");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					categoria.setNome(textNome.getName());
				}catch (Exception e1){
					JOptionPane.showMessageDialog(null, "Erro ao tentar pegar dos Texts");
				}				
				if(acao.contains("novo"))
					bd.novaCategoria(categoria);
				if(acao.contains("editar"))
					bd.editarCategoria(categoria);
				
				JOptionPane.showMessageDialog(null, "Salvo com sucesso!");
				dispose();		
				
			}
		});
		button.setBounds(253, 104, 89, 23);
		contentPane.add(button);
		
		JLabel lblCategoria = new JLabel("Categoria:");
		lblCategoria.setBounds(10, 40, 99, 14);
		contentPane.add(lblCategoria);
		
		if(acao.contains("detalhes")){
			textNome.enable(false);
		}
	}
}
